<?php
namespace Home\Controller;
use Think\Controller;
class BookkeepingController extends BackController {
    protected $limitnum = 10;
	public function Cashbook(){
        $map = array();
        //备注
        $keyword = I('keyword');
        if ($keyword){
            $map['b.mark'] = array('like','%'.$keyword.'%');
        }
        //收支类型
        $result['quiz1'] = I('quiz1');
        if ($result['quiz1']){
            $map['b.type'] = array('eq',$result['quiz1']);
        }
        //账目类型
        $result['quiz2'] = I('quiz2');
        if ($result['quiz2']){
            $map['b.sid'] = array('eq',$result['quiz2']);
        }
        //入账日期
        $result['datesign'] = I('datesign');
        if ($result['datesign']){
            $map['b.date'] = array('eq',$result['datesign']);
        }
        //所在校区
        $result['quiz3'] = I('quiz3');
        if ($result['quiz3']){
            $map['b.xid'] = array('eq',$result['quiz3']);
        }
        //收支金额
        $result['money'] = I('money');
        if ($result['money']){
            $map['b.money'] = array('eq',$result['money']);
        }
        //至今账户
        $result['quiz4'] = I('quiz4');
        if ($result['quiz4']){
            $map['b.cid'] = array('eq',$result['quiz4']);
        }

        //分页
        $result['limit'] = I('p');
        if ($result['limit'] <= 0 || empty($result['limit'])){
            $limit['limit'] = 1;
        }else{
            $limit['limit'] = $result['limit'];
        }
        $end = $limit['limit']*$this->limitnum;
        $star = ($limit['limit']-1)*$this->limitnum;
        $status = array(
            1=>"收入",
            2=>"支出"
        );
        //获取列表
        $count = M('cash_book')
            ->alias('b')
            ->join('left join school_area as s on s.id=b.xid')
            ->join('left join class_acc as c on c.id=b.sid')
            ->join('left join account_setup as a on a.id=b.cid')
            ->where($map)
            ->count();
        $data = M('cash_book')
            ->alias('b')
            ->field('b.*,s.name as sname,c.name as cname,a.name as aname')
            ->join('left join school_area as s on s.id=b.xid')
            ->join('left join class_acc as c on c.id=b.sid')
            ->join('left join account_setup as a on a.id=b.cid')
            ->where($map)
            ->limit($star,$end)
            ->select();
        $countlimit = count($data);
        for ($i=0;$i<=$countlimit-1;$i++ ){
            $data[$i]['status'] = $status[$data[$i]['type']];
        }
        //获取账目类型
        $zmdata = M('class_acc')->getField('id,name');
        //获取直径账户
        $zjdata = M('account_setup')->getField('id,name');
        //获取学校
        $xx = M('school_area')->getField('id,name');
        $Page = new \Think\Page($count, $this->limitnum);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', " <ul class='pagination pagination-large'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show(); // 分页显示输出

        $this->assign("page", $show);
        $this->assign("xx", $xx);
        $this->assign("zjdata", $zjdata);
        $this->assign("zmdata", $zmdata);
        $this->assign("status", $status);
        $this->assign("map", $result);
        $this->assign("keyword", $keyword);
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
	}
    public function CashbookDetail(){
        $result['id'] = I('id');
        $result['ff'] = I('ff');
        $status = array(
            1=>"收入",
            2=>"支出"
        );
        if ($result['id'] <= 0 ||empty($result['id'])){
            //获取账目类型
            $zmdata = M('class_acc')->getField('id,name');
            //获取直径账户
            $zjdata = M('account_setup')->getField('id,name');
            //获取学校
            $xx = M('school_area')->getField('id,name');
            $this->assign("xx", $xx);
            $this->assign("status", $status);
            $this->assign("zjdata", $zjdata);
            $this->assign("zmdata", $zmdata);
            $this->assign("flase", 1);
            $this->assign("fs", 3);
        }else{
            //获取账目类型
            $zmdata = M('class_acc')->getField('id,name');
            //获取直径账户
            $zjdata = M('account_setup')->getField('id,name');
            //获取学校
            $xx = M('school_area')->getField('id,name');
            $map['id'] = array('eq',$result['id']);
            $find = M('cash_book')->where($map)->find();
            $this->assign("xx", $xx);
            $this->assign("status", $status);
            $this->assign("zjdata", $zjdata);
            $this->assign("zmdata", $zmdata);
            $this->assign("map", $find);
            if ($result['ff']==1){
                $this->assign("fs", 1);
            }else{
                $this->assign("fs", 2);
            }


        }

        $this->display();
    }
    public function CashbookSave(){
	    $user = session('qe_user');
        $map['id'] = I('id');
        $result['type'] = I('type');
        $result['date'] = I('date');
        $result['sid'] = I('sid');
        $result['money'] = I('money');
        $result['cid'] = I('cid');
        $result['xid'] = I('xid');
        $result['mark'] = I('mark');
        $result['operation'] = $user['name'];
        if (empty($map['id'])){
            $data = M('cash_book')->add($result);
        }else{
            $data = M('cash_book')->where($map)->save($result);
        }
        if ($data){
            $this->success('操作成功！',U('Bookkeeping/Cashbook'));
        }else{
            $this->error('操作失败');die;
        }
    }
    public function CashbookDel(){
        $map['id'] = I('id');
        $result['status'] = I('status');
        $data = M('cash_book')->where($map)->delete($result);
        if ($data){
            $this->success('操作成功！',U('Bookkeeping/Cashbook'));
        }else{
            $this->error('操作失败');die;
        }
    }


	public function IncomeStatistics(){
	    //1年/ 2月
        $flag = I('flag');

        $result['sid'] = I('quiz2');
        $result['xid'] = I('quiz3');
        if ($result['sid'] == 'quiz3'){
            $result['sid'] = '';
        }

        if ($flag ==2 ){
            $time = "%Y%m";
            $days = date("t");
            for ($i=1;$i<=$days;$i++){
                $day[$i] = (string)$i;
            }
            $where = "DATE_FORMAT(b.date,'".$time."') = DATE_FORMAT(CURDATE(),'".$time."')";
        }elseif($flag ==3){
            $where = "b.money > 0";
        }else{
            $flag = 1;
            for ($i=1;$i<=12;$i++){
                $day[$i] = (string)$i;
            }
            $time = "%Y";
            $where = "DATE_FORMAT(b.date,'".$time."') = DATE_FORMAT(CURDATE(),'".$time."')";
        }
        if ($result['sid']){
            $where .=" and b.sid=".$result['sid'];
        }
        if ($result['xid']){
            $where .=" and b.xid=".$result['xid'];
        }
        //获取账目类型
        $zmdata = M('class_acc')->getField('id,name');
        //获取学校
        $xx = M('school_area')->getField('id,name');
        //获取全部分类账目
        //本月天数
        $map = array();
        //获取本月全部数据
        $data = M('cash_book')
            ->alias('b')
            ->field("b.*")
            ->join('left join school_area as s on s.id=b.xid')
            ->join('left join class_acc as c on c.id=b.sid')
            ->where($where)
            ->order('b.date desc')
            ->select();
        if ($flag == 3){
            $minyear = explode('-',end($data)['date'])[0];
            $year = date('Y',time());
            for ($i=$minyear;$i<=$year;$i++){
                $day[$i] = $i;
            }
            $flag = 0;
        }

        //统计本月/年
        $daymon = array();
        foreach ($data as $k=>$v){
          $date  = explode('-',$v['date']);
          foreach ($day as $kk=>$vv){
              if ($date[$flag] == $vv){
                  $daymon[$vv] +=$v['money'];
              }
          }
        }
        $sum = '';
        $strday = '';
        foreach ($day as $kkk=>$vvv){
            if ($daymon[$vvv]){
                $sum .=  (string)$daymon[$vvv].",";
            }else{
                $sum .= "0,";
            }
            $strday .= $vvv.",";
        }

        $this->assign("sum", json_encode($sum));
        $this->assign("day", json_encode($strday));
        $this->assign("xx", $xx);
        $this->assign("flag", $flag);
        $this->assign("map", $result);
        $this->assign("zmdata", $zmdata);
		$this->display();
	}
	public function AccountsCassify(){
	    $map = array();
        //高级搜索
        $keyword = I('keyword');
        if ($keyword){
            $map['name'] = array('like','%'.$keyword.'%');
        }
        //分类类型
        $result['type'] = I('type');
        if ($result['type']){
            $map['type'] = array('eq',$result['type']);
        }
        //开启状态
        $result['status'] = I('status');
        if ($result['status']){
            $map['status'] = array('eq',$result['status']);
        }
        //分页
        $result['limit'] = I('p');
        if ($result['limit'] <= 0 || empty($result['limit'])){
            $limit['limit'] = 1;
        }else{
            $limit['limit'] = $result['limit'];
        }
        $end = $limit['limit']*$this->limitnum;
        $star = ($limit['limit']-1)*$this->limitnum;
        $type = array(
          1=>"收入",
          2=>"支出"
        );
        $status = array(
          1=>"启用",
          2=>"禁用"
        );
		//获取列表
        $count = M('class_acc')
            ->where($map)
            ->count();
        $data = M('class_acc')
            ->where($map)
            ->select();
        $countlimit = count($data);
        for ($i=0;$i<=$countlimit-1;$i++ ){
            $data[$i]['type'] = $type[$data[$i]['type']];
            $data[$i]['status'] = $status[$data[$i]['status']];
        }
        $Page = new \Think\Page($count, $this->limitnum);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', " <ul class='pagination pagination-large'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show(); // 分页显示输出

        $this->assign("page", $show);
        $this->assign("type", $type);
        $this->assign("status", $status);
        $this->assign("map", $result);
        $this->assign("keyword", $keyword);
        $this->assign("count", $count);
        $this->assign("data", $data);
		$this->display();
	}
    public function AccountsCassifyDetail(){
        $result['id'] = I('id');
        if ($result['id'] <= 0 ||empty($result['id'])){
            $this->error('操作失败');die;
        }
        $type = array(
            1=>"收入",
            2=>"支出"
        );
        $status = array(
            1=>"启用",
            2=>"禁用"
        );
        $map['id'] = array('eq',$result['id']);
        $find = M('class_acc')->where($map)->find();
        $this->assign("type", $type);
        $this->assign("data", $find);
        $this->assign("status", $status);
        $this->display();
    }
    public function AccountsCassifySave(){
        $map['id'] = I('id');
        $result['name'] = I('name');
        $result['type'] = I('type');
        $result['status'] = I('status');
        $data = M('class_acc')->where($map)->save($result);
        if ($data){
            $this->success('操作成功！',U('Bookkeeping/AccountsCassify'));
        }else{
            $this->error('操作失败');die;
        }
    }
    public function AccountsCassifyDel(){
        $map['id'] = I('id');
        $result['status'] = I('status');
        $data = M('class_acc')->where($map)->delete($result);
        if ($data){
            $this->success('操作成功！',U('Bookkeeping/AccountsCassify'));
        }else{
            $this->error('操作失败');die;
        }
    }
	public function AccountSetup(){
        $map = array();
        //高级搜索
        $keyword1 = I('keyword1');
        if ($keyword1){
            $map['name'] = array('like','%'.$keyword1.'%');
        }
        $keyword2 = I('keyword2');
        if ($keyword2){
            $map['mark'] = array('like','%'.$keyword2.'%');
        }
        //开启状态
        $result['status'] = I('status');
        if ($result['status']){
            $map['status'] = array('eq',$result['status']);
        }
        //分页
        $result['limit'] = I('p');
        if ($result['limit'] <= 0 || empty($result['limit'])){
            $limit['limit'] = 1;
        }else{
            $limit['limit'] = $result['limit'];
        }
        $end = $limit['limit']*$this->limitnum;
        $star = ($limit['limit']-1)*$this->limitnum;
        $status = array(
            1=>"启用",
            2=>"禁用"
        );
        //获取列表
        $count = M('account_setup')
            ->where($map)
            ->count();
        $data = M('account_setup')
            ->where($map)
            ->limit($star,$end)
            ->select();
        $countlimit = count($data);
        for ($i=0;$i<=$countlimit-1;$i++ ){
            $data[$i]['status'] = $status[$data[$i]['status']];
        }
        $Page = new \Think\Page($count, $this->limitnum);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', " <ul class='pagination pagination-large'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show(); // 分页显示输出

        $this->assign("page", $show);
        $this->assign("status", $status);
        $this->assign("map", $result);
        $this->assign("keyword1", $keyword1);
        $this->assign("keyword2", $keyword2);
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
	}
    public function AccountSetupDetail(){
        $result['id'] = I('id');
        if ($result['id'] <= 0 ||empty($result['id'])){
            $this->error('操作失败');die;
        }
        $status = array(
            1=>"启用",
            2=>"禁用"
        );
        $map['id'] = array('eq',$result['id']);
        $find = M('account_setup')->where($map)->find();
        $this->assign("data", $find);
        $this->assign("status", $status);
        $this->display();
    }
    public function AccountSetupSave(){
        $map['id'] = I('id');
//        dump($map);
        $result['name   '] = I('name');
        $result['mark'] = I('mark');
        $result['status'] = I('status');
//        dump($result);die;
        $data = M('account_setup')->where($map)->save($result);
        if ($data){
            $this->success('操作成功！',U('Bookkeeping/AccountSetup'));
        }else{
            $this->error('操作失败');die;
        }
    }
    public function AccountSetupDel(){
        $map['id'] = I('id');
        $result['status'] = I('status');
        $data = M('account_setup')->where($map)->delete($result);
        if ($data){
            $this->success('操作成功！',U('Bookkeeping/AccountSetup'));
        }else{
            $this->error('操作失败');die;
        }
    }
}